package com.mujin.keji.collectionsystem.module.outboundtask.views;

import android.Manifest;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.CcsApplication;
import com.mujin.keji.collectionsystem.Constant;
import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseActivity;
import com.mujin.keji.collectionsystem.base.BaseDto;
import com.mujin.keji.collectionsystem.module.login.dto.LoginDto;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.CallDto;
import com.mujin.keji.collectionsystem.module.outboundtask.viewmodel.OneCallViewModel;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;
import com.mujin.keji.collectionsystem.utils.ImageUtils;
import com.mujin.keji.collectionsystem.utils.SPUtil;
import com.mujin.keji.collectionsystem.utils.ToastUtils;

/**
 * 一键报警界面
 *
 * @author weishixiong
 * @Time 2018-05-22
 */

public class OneCallActivity extends BaseActivity {
    private ImageView ivOneCall;
    private ImageView ivHead;
    private ImageView ivHangUp;
    private OneCallViewModel viewModel;
    private TextView tvCallStatus;
    private TextView tvPhoneNumber;
    private String phone;
    private String answering;//通话对象 string
    private String calleeNumber;// 被叫号码 string @mock=15277432764
    private String caseId;//案件id number
    private String relation;//关系 string

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CcsApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CcsApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(OneCallViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_onecall;
    }

    @Override
    public void initView() {

        setleftImage(R.drawable.image_arrow_left_white, true, null);
        LoginDto dto = SPUtil.getData(Constant.SP.SP, Constant.SP.LOGIN_KEY, LoginDto.class, null);
        ivOneCall = (ImageView) findViewById(R.id.iv_one_call);
        ivHead = (ImageView) findViewById(R.id.iv_head);
        ivHangUp = (ImageView) findViewById(R.id.iv_hang_up);
        tvCallStatus = (TextView) findViewById(R.id.tv_call_status);
        ImageUtils.loadImage(this, R.drawable.image_one_call_gif, ivOneCall);
        tvPhoneNumber = (TextView) findViewById(R.id.tv_phone_number);
        if (EmptyUtils.isNotEmpty(dto) && EmptyUtils.isNotEmpty(dto.getImg())) {
            ImageUtils.loadImage(this, dto.getImg(), ivHead);
        }

        if (TextUtils.isEmpty(getIntent().getStringExtra("phoneNumber"))) {
            phone = "18665986512";
            setCenterTitle("一键报警", "#FFFFFF");
            tvPhoneNumber.setText("110");
        } else {
            phone = getIntent().getStringExtra("phoneNumber");
            answering = getIntent().getStringExtra("answering");
            caseId = getIntent().getStringExtra("caseId");
            relation = getIntent().getStringExtra("relation");
            setCenterTitle("通话", "#FFFFFF");
            tvPhoneNumber.setText(getIntent().getStringExtra("phoneNumber"));
        }

    }

    @Override
    public void initData() {

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(this.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            ToastUtils.showToast("未获取到电话权限,无法使用一键呼叫");
            return;
        }
        String imei = telephonyManager.getDeviceId();
        viewModel.getLiveData(phone, imei, answering, caseId, relation).observe(this, new Observer<BaseDto<CallDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<CallDto> callDtoBaseDto) {
                if (callDtoBaseDto.getCode() == Constant.Server.SUCCESS_CODE) {
                    tvCallStatus.setText("已接听");
                } else {
                    finish();
                    ToastUtils.showToast(callDtoBaseDto.getMsg());
                }
            }
        });
    }

    @Override
    public void initListener() {
        ivHangUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}
